package com.java1412.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.java1412.entity.Student;
import com.java1412.service.StudentService;
import jakarta.annotation.Resource;
import org.apache.catalina.User;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author java1412
 * @since 2025-05-30 15:34
 */
@RestController
@RequestMapping(value = "/student")
@CrossOrigin    // 允许跨域访问，但是不能携带请求
public class StudentController {
    @Resource
    private StudentService studentService;

    @GetMapping(value = "/find")
    public List<Student> find(
            @RequestParam(value = "name") String name
    ) {
        // return studentService.findAll();
        // return studentService.list();
//        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
//        studentQueryWrapper.like("name", name);
        // return studentService.list(studentQueryWrapper);
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Student::getName, name);
        return studentService.list(wrapper);
    }

    @GetMapping(value = "/{id}")
    public Student findById(@PathVariable("id") Integer id) {
        // return studentService.findById(id);
        return studentService.getById(id);
    }

    @GetMapping
    public List<Student> findAll() {
        // return studentService.findAll();
        return studentService.list();
    }

    @PostMapping(value = "/add")
    public Map<String, Object> add(Student student) {
        // return studentService.add(student);
        studentService.save(student);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功");
        return map;
    }

    @PostMapping(value = "/update")
    public Map<String, Object> update(Student student) {
        // return studentService.update(student);
        studentService.updateById(student);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功");
        return map;
    }

    @GetMapping(value = "/delete/{id}")
    public Map<String, Object> deleteById(@PathVariable("id") Integer id) {
        // return studentService.deleteById(id);
        studentService.removeById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功");
        return map;
    }
}
